package org.weasel.wizards;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.Wizard;
import org.weasel.business.Developer;
import org.weasel.business.IDFactory;
import org.weasel.business.Iteration;
import org.weasel.business.Project;
import org.weasel.business.Release;
import org.weasel.business.Unscheduled;
import org.weasel.controllers.ApplicationController;
import org.weasel.controllers.TreeController;
import org.weasel.models.Model;
import org.weasel.tree.TreeObjectIteration;
import org.weasel.tree.TreeObjectProject;
import org.weasel.tree.TreeObjectRelease;
import org.weasel.tree.TreeObjectUnscheduled;
import org.weasel.tree.TreeParent;

public class ProjectWizard extends Wizard {
	private BasicProjectPage pageProjectName;
	private SettingsProjectPage pageReleasesAndIterations;
	private ISelection selection;
	private Model model;

	
	public ProjectWizard(Model model) {
		super();
		setWindowTitle("New Project");
		setNeedsProgressMonitor(true);
		this.model = model;
	}
	
	@Override
	public void addPages() {
        pageProjectName = new BasicProjectPage(selection);
        addPage(pageProjectName);
        pageReleasesAndIterations = new SettingsProjectPage(selection, model);
        addPage(pageReleasesAndIterations);
    }

	@Override
	public boolean performFinish() {
		Project project = null;
		TreeObjectProject to_project = null;
		try {
			String projectName = pageProjectName.getProjectName();
			
			project = new Project(new IDFactory().getUniqueId(), projectName, null, null, null, "", pageReleasesAndIterations.getProjectStartDate(), Integer.parseInt(pageReleasesAndIterations.getDurationOfIterations()));
			to_project = new TreeObjectProject(projectName, project);
			for (Developer developer : pageReleasesAndIterations.getDevelopers()) {
				project.addDeveloper(developer);
			}
			
			int numberOfReleases = Integer.parseInt(pageReleasesAndIterations.getNumberOfReleases());
			int numberOfIterations = Integer.parseInt(pageReleasesAndIterations.getIterationsPerRelease());
			
			TreeParent to_releases = new TreeParent("Releases");
			to_project.addChild(to_releases);
			
			for (int i = 0; i < numberOfReleases; i++) {
				String releaseNumber = String.valueOf(i + 1);
				Release aRelease = new Release(new IDFactory().getUniqueId(), "Release " + releaseNumber, null, null, "");
				TreeObjectRelease to_release = new TreeObjectRelease(aRelease.getName(), aRelease);
				to_releases.addChild(to_release);
				
				for (int j = 0; j < numberOfIterations; j++) {
					String iterationNumber = String.valueOf(j + 1);
					Iteration iteration = new Iteration(new IDFactory().getUniqueId(), "Iteration " + iterationNumber, null, "");
					aRelease.addIteration(iteration);
					TreeObjectIteration to_iteration = new TreeObjectIteration(iteration.getName(), iteration);
					to_release.addChild(to_iteration);
				}
				Unscheduled releaseUnscheduled = new Unscheduled(new IDFactory().getUniqueId(), null);
				aRelease.setUnscheduled(releaseUnscheduled);
				TreeObjectUnscheduled to_releaseUnscheduled = new TreeObjectUnscheduled("Unscheduled", releaseUnscheduled);
				to_release.addChild(to_releaseUnscheduled);
				
				project.addRelease(aRelease);
			}
			
			Unscheduled projectUnscheduled = new Unscheduled(new IDFactory().getUniqueId(), null);
			project.setUnscheduled(projectUnscheduled);
			TreeObjectUnscheduled to_projectUnscheduled = new TreeObjectUnscheduled("Unscheduled", projectUnscheduled);
			to_project.addChild(to_projectUnscheduled);
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		TreeController.instance().addProject(project, to_project);
		//do date calculations
		if(pageReleasesAndIterations.getProjectStartDate()!=null && !pageReleasesAndIterations.getProjectStartDate().equals("")){
			//TODO do this in a nicer way...
			ApplicationController.instance().calculateDates();
		}
		return true;
	}



}
